var map = new AMap.Map('container');
var app = new Vue({
    el: '#app',
    data: {
        openAccount: 0,// 0已开户，1 未开户，2开户中
        timer: null
    },
    mounted() {
        window.history.forward();
        // if (localStorage.getItem('registering')) {
        //     this.getAccountInfo(2);
        // } else if (localStorage.getItem('registered')) {
        //     localStorage.removeItem('registering');
        //     this.getAccountInfo(0);
        // } else {
        //     this.getAccountInfo(1);
        // }
        this.getAccountInfo(1);

    },
    beforeDestory() {
        this.$indicator.close();
        this.timer && clearTimeout(this.timer);
    },
    methods: {
        getAccountInfo(openAccount = 1) {
            utils.clearLocalStorage();
            this.$indicator.open({
                text: '身份信息检测中，请稍后',
                spinnerType: 'fading-circle'
            });

            var _this = this;
            // 用户处于未开卡的时候需要获取一下位置信息
            _this.timer = setTimeout(() => {
                _this.openAccount = openAccount;
                switch (openAccount) {
                    case 0:
                    default:
                        _this.jump('myAccount');
                        break;
                    case 1:
                        this.getLocationInfo(function () {
                            _this.jump('register');
                        });
                        break;
                    case 2:
                        _this.$indicator.close();
                        _this.$indicator.open({
                            text: '开户中...',
                            spinnerType: 'fading-circle'
                        })
                }
            }, 1000);
        },
        jump(url, query) {
            utils.jump(url, query);
        },
        getLocationInfo: function (callback) {
            this.$indicator.open({
                text: '获取地理位置信息中，请稍后',
                spinnerType: 'fading-circle'
            });
            var _this = this;
            map.plugin('AMap.Geolocation', function () {
                var geolocation = new AMap.Geolocation({
                    // 是否使用高精度定位，默认：true
                    enableHighAccuracy: true,
                    // 设置定位超时时间，默认：无穷大
                    timeout: 10000,
                    // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
                    buttonOffset: new AMap.Pixel(10, 20),
                    //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
                    zoomToAccuracy: true,
                    //  定位按钮的排放位置,  RB表示右下
                    buttonPosition: 'RB'
                })

                geolocation.getCurrentPosition()
                AMap.event.addListener(geolocation, 'complete', onComplete)
                AMap.event.addListener(geolocation, 'error', onError)

                function onComplete(data) {
                    // data是具体的定位信息
                    _this.$indicator.close();
                    var locationinfo = data.addressComponent;
                    var position = data.position
                    const addressInfo = {
                        city: locationinfo.city,
                        district: locationinfo.district,
                        province: locationinfo.province,
                        lat: position.lat,
                        lng: position.lng
                    }
                    utils.setItem('locationInfo', addressInfo);
                    console.log(data);
                    _this.$toast(`定位成功，当前位置${data.formattedAddress}`);
                    callback();
                }

                function onError(data) {
                    // 定位出错
                    _this.$indicator.close();
                    _this.$toast('定位失败，请稍后再试');
                }
            });
        }
    }
});
